package com.example.shopping.controller;

import com.example.shopping.pojo.Brand;
import com.example.shopping.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*
Create by 苍狼 于 2020/1/16
站在巅峰看世界，
落在深渊思人生。
*/
@Controller
@CrossOrigin
@Api("品牌页面Api")
public class BrandController {
    private int pageNum = 0;

    @Autowired(required = false)
    private BrandService brandService;

    //全部查询信息
    @RequestMapping("/selectBrandList")
    @ApiOperation(value = "品牌全部查询")
    // @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")

    public String selectBrandList(HttpServletRequest request) {
        //设置开始页
//        PageHelper.startPage(pageNum, 10);
        List<Brand> brandList = brandService.selectListBrand();
        //分页
//        PageInfo<Brand> pageInfo = new PageInfo<Brand>(brandList);
//        pageInfo.setPageNum(pageNum);
        request.getSession().setAttribute("brandList", brandList);
        return "brand";
    }

    @ApiOperation(value = "删除品牌信息", notes = "根据id来指定删除对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
//            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    })
    //删除，删除之后全部查询
    @RequestMapping("/deleteIdBrand")
    public String deleteIdBrand(int id) {
        brandService.deleteIdBrand(id);
        return "redirect:/selectBrandList";
    }

    //新增页面
    @RequestMapping("/addBrand")
    @ApiOperation(value = "添加品牌信息")
    @ApiImplicitParams(@ApiImplicitParam(name = "brand", value = "添加的品牌信息", required = true, dataType = "Brand"))
    public String addBrand() {
        return "insertBrand";
    }

    //新增方法
    @RequestMapping("/insertBrand")
    @ApiOperation(value = "添加品牌信息")
    @ApiImplicitParams(@ApiImplicitParam(name = "brand", value = "添加的品牌信息", required = true, dataType = "Brand"))
    public String insertBrand(Brand brand) {
        int i = brandService.insertBrand(brand);
        if (i > 0) {
            return "redirect:selectBrandList";
        }
        return "insertBrand";
    }

    //修改 如果成功全部展示，不成功继续修改
    @RequestMapping("/updateBrand")
    @ApiOperation(value = "修改品牌信息", response = Brand.class, notes = "修改品牌信息Api")
    @ApiImplicitParam(name = "brand", value = "修改的品牌实体类信息", required = true, dataType = "Brand")
    public String updateBrand(Brand brand) {
        int i = brandService.updateBrand(brand);
        if (i > 0) {
            return "redirect:/selectBrandList";
        }
        return "updateBrand";
    }

    //修改页面
    @RequestMapping("/updatePage")
    public String updatePage(int id, HttpServletRequest request) {
        Brand brand = brandService.selectByOne(id);
        request.getSession().setAttribute("brand", brand);
        return "updateBrand";
    }


    //单个查询
    @RequestMapping("/selectByOne")
    @ApiOperation(value = "单个查询品牌信息")
    @ApiImplicitParam(name = "id", value = "根据ID查询", required = true, dataType = "Long")
    public String selectByOne(int id, HttpServletRequest request) {
        Brand brand = brandService.selectByOne(id);
        request.getSession().setAttribute("brand", brand);
        return "updatePage";
    }

    //模糊查询
    @RequestMapping("/queryLikeBrand")
    @ApiOperation(value = "模糊查询")
    @ApiImplicitParam(name = "brand", value = "根据实体类对象模糊查询", required = true, dataType = "Brand")
    public String queryLikeBrand(Brand brand, HttpServletRequest request) {
        List<Brand> brandList = brandService.queryLikeBrand(brand);
        request.getSession().setAttribute("brandList", brandList);
        return "brand";
    }


}
